﻿using LiteDB;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LiteDb
{
    // 端口扩展
    public class PortExtra
    {
        public enum HwDevType
        {
            serialport = 0,
            ethernet = 1,
            system = 2
        }

        public class ConvertInfo
        {
            public int com { get; set; } = -1;
            public int port { get; set; } = 8080;
            public bool enable { get; set; } = true;
        }

        public class OpcInfo
        {
            public int loginType { get; set; } = 0;
            public string endpointURL { get; set; } = " ";
            public string usrName { get; set; } = " ";
            public string password { get; set; } = " ";
            public string certificate { get; set; } = " ";
            public string key { get; set; } = " ";
        }

        public class RemoteSetting
        {
            public string address { get; set; } = " ";
            public string port { get; set; } = " ";
            public string reportPackage { get; set; } = " ";
            public string reportHead { get; set; } = " ";
            public string reportTail { get; set; } = " ";
            public string heartTime { get; set; } = " ";
        }

        public class Params
        {
            public int enable { get; set; } = 1;
            public int type { get; set; } = 0;
            public int device_type { get; set; } = 0;
            public int port { get; set; } = 0;
            public int baud { get; set; } = 9600;
            public string parity { get; set; } = "None";
            public int bits { get; set; } = 8;
            public int stop { get; set; } = 1;
            public int resp_time_out_ms { get; set; } = 3000;
            public int sample_interval_s { get; set; } = 30;
            public int frame_interval_ms { get; set; } = 300;
            public string ip_addr { get; set; } = "127.0.0.1";
            public int ip_port { get; set; } = 8080;
            public ConvertInfo convert { get; set; } = new ConvertInfo();
            public OpcInfo opc { get; set; } = new OpcInfo();
            public RemoteSetting remote { get; set; } = new RemoteSetting();
        }

        public int id { get; set; } = -1;
        public string name { get; set; } = "port";
        public int group_device_id { get; set; } = -1;
        public int proto_type { get; set; } = (int)HwDevType.serialport;
        public Params attrs { get; set; } = new Params();

        //  需要写一个转换函数(把object转换为PortExtra)
        public static PortExtra Convert(object obj)
        {
            // obj是一个json字符串，转换为PortExtra，需要捕获异常
            try
            {
                if (obj == null)
                {
                    throw new ArgumentNullException(nameof(obj), "输入对象不能为空");
                }

                var json = obj.ToString();
                if (string.IsNullOrWhiteSpace(json))
                {
                    throw new ArgumentException("输入的JSON字符串不能为空或仅包含空白字符", nameof(obj));
                }

                var extra = JsonConvert.DeserializeObject<PortExtra>(json);
                if (extra == null)
                {
                    throw new Exception("反序列化结果为null");
                }

                return extra;
            }
            catch (Exception ex)
            {
                throw new Exception("转换失败: " + ex.Message);
            }
        }
    }
}